<?php
//1.获取连接
$url = "localhost:3306";
$username = "root";
$password = "";
$database = "php";
$conn = mysqli_connect($url,$username,$password,$database);
// var_dump($conn);
if(!$conn){
    echo "获取连接失败:".mysqli_connect_error()."<br>";
    echo "错误码:".mysqli_connect_errno()."<br>";
}
// 连接成功
// 建议设置连接的字符集, 防止中文乱码
mysqli_query($conn, "set names utf8");

$p_name = '西瓜';

$sql = "insert into t_product (p_name) values ('$p_name')";
// 2.执行sql语句
// 执行 增删改 成功后, 返回true, 否则返回false
$result = mysqli_query($conn, $sql);
// 3.处理结果
if($result){
    echo "<h2>执行成功</h2>";
    echo "当前商品的p_id:" . mysqli_insert_id($conn);
} else {
    echo "执行失败:" . mysqli_error($conn) . "<br>";
    echo "错误码:" . mysqli_errno($conn) . "<br>";
}
// 4.关闭连接(释放资源)
mysqli_close($conn);

?>